<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import { $t } from '@/locales';
import { enableStatusOptions } from '@/constants/business';
import { saveRoom } from '@/service/api/room';

defineOptions({
  name: 'RoomOperateDrawer'
});

interface Props {
  /** the type of operation */
  operateType: AntDesign.TableOperateType;
  /** the edit row data */
  rowData?: Api.SystemManage.Room | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const { formRef, validate, resetFields } = useAntdForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<AntDesign.TableOperateType, string> = {
    add: '新增阅览室',
    edit: '编辑阅览室'
  };
  return titles[props.operateType];
});

type Model = Pick<Api.SystemManage.Room, 'floor' | 'roomName' | 'row' | 'column' | 'state'>;

const model = ref(createDefaultModel());

function createDefaultModel(): any {
  return {
    floor: null,
    roomName: null,
    row: null,
    column: null,
    state: '1'
  };
}

type RuleKey = Exclude<keyof Model, 'roleDesc'>;

const rules: Record<RuleKey, App.Global.FormRule> = {
  floor: defaultRequiredRule,
  roomName: defaultRequiredRule,
  state: defaultRequiredRule,
  row: defaultRequiredRule,
  column: defaultRequiredRule
};

function handleInitModel() {
  model.value = createDefaultModel();

  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model.value, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

async function handleSubmit() {
  await validate();
  // request
  await saveRoom(model.value);
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
    resetFields();
  }
});
</script>

<template>
  <ADrawer v-model:open="visible" :title="title" :width="360">
    <AForm ref="formRef" layout="vertical" :model="model" :rules="rules">
      <AFormItem label="楼号" name="floor">
        <AInput v-model:value="model.floor" placeholder="请输入楼号" />
      </AFormItem>
      <AFormItem label="阅览室名称" name="roomName">
        <AInput v-model:value="model.roomName" placeholder="请输入阅览室名称" />
      </AFormItem>
      <AFormItem label="阅览室状态" name="state">
        <ARadioGroup v-model:value="model.state">
          <ARadio v-for="item in enableStatusOptions" :key="item.value" :value="item.value">
            {{ $t(item.label) }}
          </ARadio>
        </ARadioGroup>
      </AFormItem>
      <AFormItem label="座位行数" name="row">
        <AInputNumber v-model:value="model.row" style="width: 144px" placeholder="请输入座位行数" />
      </AFormItem>
      <AFormItem label="座位列数" name="col">
        <AInputNumber v-model:value="model.column" style="width: 144px" placeholder="请输入座位列数" />
      </AFormItem>
    </AForm>
    <template #footer>
      <div class="flex-y-center justify-end gap-12px">
        <AButton @click="closeDrawer">{{ $t('common.cancel') }}</AButton>
        <AButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</AButton>
      </div>
    </template>
  </ADrawer>
</template>

<style scoped></style>
